/**
 * Copyright © 2009 Qasky. All rights reserved.
 *
 * 用户状态管理
 *
 * @author Wang Hongya
 * @version 0.0.0 2019/10/14
 *
 * History:
 * (author, date, desc)
 * (Wang Hongya, 20191014, 添加注释)
 */

import {loadResources,loadResource,delSysResource,addSysResource,updSysResource,enableSysResources,disableSysResources} from 'api/sysResManagerApi';
import util from "@/libs/util";

/* 用户的状态管理 */
const sysResManager = {
    state: {
        resourceTree: []
    },
     mutations: {
       resourceTreeMut(state, obj) {
         util.setTreeCustom(obj)
         state.resourceTree = obj;
       }
     },
     actions: {
      //加载树结构 并返还被选中节点的id
       loadResourceTreeAct (context,param) {
         return loadResources().then(response => {
           let data = response.data.data;
           console.log('demo测试测试')
           console.log(data)
           let retId = null;
           if(param.selectedId){
             retId =  param.selectedId;
             util.handleSelectedTree(data,param.selectedId);
           }else {
             data.forEach(item=>{
               if(item.selected){
                 retId = item.id;
               }
             });
           }
           context.commit('resourceTreeMut', data);
           return retId;
         });
       },
       loadResourceAct (context,parm) {
         return loadResource(parm).then(response => {
             return response.data;
         });
       },
       /*删除系统资源*/
       delSysResourceAct (context, para) {
         return delSysResource(para).then(response => {
           let data = response.data;
           return data;
         });
       },
       /*新增系统资源*/
       addSysResourceAct (context, para) {
         return addSysResource(para).then(response => {
           let data = response.data;
           return data;
         });
       },
       /*更新系统资源*/
       updSysResourceAct (context, para) {
         return updSysResource(para).then(response => {
           let data = response.data;
           return data;
         });
       },
       /*新增系统资源*/
       enableSysResourceAct (context, para) {
         return enableSysResources(para).then(response => {
           let data = response.data;
           return data;
         });
       },
       /*更新系统资源*/
       disableSysResourceAct (context, para) {
         return disableSysResources(para).then(response => {
           let data = response.data;
           return data;
         });
       }
    }
};

export default sysResManager;
